#include "sdb.h"
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#define NR_BP 20

typedef struct breakpoint {
  uint32_t pc;
  struct breakpoint *next;
} BP;

static BP* head = NULL, *tail = NULL;

void new_bp(uint32_t pc) {
  if (head == NULL) {
    head = (BP *)malloc(sizeof(BP));
    head->pc = pc;
    head->next = NULL;

    tail = head;
  }
  else {
    BP* tmp = (BP *)malloc(sizeof(BP));
    tmp->pc = pc;
    tmp->next = NULL;

    tail->next = tmp;
    tail = tail->next;
  }
}

void free_bp() {
  panic("No implementation for freeing breakpoint!!!");
}

bool bp_empty() {
  if (head == NULL) return true;
  else return false;
}

bool bp_match(uint32_t pc) {
  bool match = false;

  BP* tmp = head;
  while (tmp != NULL) {
    if (tmp->pc == pc) {
      match = true;
      break;
    }
    tmp = tmp->next;
  }

  return match;
}

void bp_clear() {
  while (head != NULL) {
    BP* tmp = head;
    head = head->next;

    free(tmp);
  }
}

void bp_display() {
  BP* tmp = head;

  int i = 1;
  while (tmp != NULL) {
    printf("breakpoint %d: 0x%08x\n", i, tmp->pc);
    tmp = tmp->next;
  }
}
